<?php
declare (strict_types = 1);

namespace app\school\service\school\admin;

use app\common\service\BaseService;
use app\school\logic\school\admin\SchoolSplitConfig as SchoolSplitConfigLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 机构分成配置-后台
 */
class SchoolSplitConfig extends BaseService
{
    /**
     * 机构等级列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @return   [type]                         [description]
     */
    public function index()
    {
        $map = [
            'school' => [],
        ];
        // 搜索功能
        Request::has('title', 'get', true) && $map['school'][] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $data                                                  = app(SchoolSplitConfigLogic::class)->getList($map);
        // 定义字段
        $keyList = [
            'title'       => ListItem::make('custom')->title('机构'),
            'status'      => ListItem::make('custom')->title('状态'),
            'update_time' => ListItem::make()->title('最后操作时间'),
        ];

        $searchKey = [
            'title' => FormItem::make('input')->title(false)->placeholder('机构名称')->maxlength(30),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // 搜索字段
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 机构配置界面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @param    Request                        $request [description]
     * @return   [type]                                  [description]
     */
    public function config()
    {
        $data = Request::post();
        $re   = app(SchoolSplitConfigLogic::class)->applySplitConfig($data);

        return $this->parseToData($re, 1, '申请成功，请等待审核');

    }

    /**
     * 审核
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-27
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function examine(int $id)
    {
        $data = Request::post();
        app(SchoolSplitConfigLogic::class)->doExamine($id, $data);

        return $this->parseToData([], 1, '审核完成');
    }

}
